/**
 * axios封装
 * 请求拦截、响应拦截、错误统一处理
 */

// export const baseurl = 'http://localhost:9090'; // 本地
// export const baseurl = 'http://121.37.242.119:9090' // Linux服务器
export const baseurl = 'https://drawu.wlmtsys.com' // Linux服务器
// const baseurl = 'http://43.139.147.77:9090' // Windows服务器

const exceptUrl = ['/forum/page', '/game/page']; // 不需要token的接口
const touristToken = 'touristToken' // 游客token
const touristUserId = 99999 // 游客userId

const request = (url, data = {}, method = 'GET') => {
	const token = uni.getStorageSync('token');
	const userId = uni.getStorageSync('userId');

	if (token || exceptUrl.includes(url)) {
	    return new Promise((resolve, reject) => {
	        uni.request({
	            url: baseurl + url, //接口地址
	            data,
	            method,
	            header: {
	                'content-type': 'application/json',
                    authentication: token || touristToken,
	                userId: userId || touristUserId
	            },
	            success: (res) => {
	                resolve(res.data)
	            },
	            fail: (res) => {
	                console.error(res)
	                reject(res)
	            }
	        })
	    })
	} else {
	    uni.showToast({
	        title: '请先登录',
	        icon: 'none'
	    })
	    uni.navigateTo({
	        url: '/pages/Login/LoginPage'
	    })
	}
};

export default request;